<template>
  <div class="goods-image">
    <div class="large"
    :style="{
    backgroundImage:`url(${imgList[curIndex]})`,
    backgroundPositionX:positionX+'px',
    backgroundPositionY:positionY+'px'
    }"
    v-if="isOutside===false"
    ></div>
    <div class="middle" ref="target">
      <!-- 图片 -->
      <img :src="imgList[curIndex]" alt="">
      <!-- 蒙层容器 -->
      <div class="layer" :style="{left:left+'px',top:top+'px'}"  v-if="isOutside===false"></div>
    </div>
    <ul class="small">
      <li
      v-for="(img,i) in imgList"
      :key="i" @mouseenter="mouseEnterFn(i)"
      :class="{active:curIndex===i}"
      >
        <img :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imgList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  setup () {
    // tab栏切换 鼠标移入哪个小图 就显示相应的大图 用 鼠标移入事件 和curIndex 数组取值
    const curIndex = ref(0) // 当前点击的index
    const left = ref(0) // 蒙层左边的位置
    const top = ref(0) // 蒙层上边的位置
    const positionX = ref(0) // 大图背景位置
    const positionY = ref(0) // 大图背景位置
    const target = ref(null)
    function mouseEnterFn (index) {
      curIndex.value = index
    }

    // 获取鼠标相对于元素的位置
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 监听鼠标位置的变化
    watch([elementX, elementY], () => {
      // 最大临界值判断
      if (elementX.value > 300) {
        left.value = 200
      }
      if (elementX.value < 100) {
        left.value = 0
      }
      if (elementY.value < 100) {
        top.value = 0
      }
      if (elementY.value > 300) {
        top.value = 200
      }
      // 有效范围内跟随移动  还是要限制一下在有效范围内 不然上面刚限制了  下面就又改了
      if (elementX.value < 300 && elementX.value > 100) {
        left.value = elementX.value - 100
      }
      if (elementY.value > 100 && elementY.value < 300) {
        top.value = elementY.value - 100
      }

      // 控制大图北京的位置
      positionX.value = -left.value * 2
      positionY.value = -top.value * 2
    })

    return {
      curIndex,
      mouseEnterFn,
      elementX,
      elementY,
      isOutside,
      left,
      top,
      positionX,
      positionY,
      target
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index:500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

}
</style>
